package com.supinfo.sun.sungamers.gui.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import com.supinfo.sun.sungamers.exceptions.UserAttemptException;
import com.supinfo.sun.sungamers.games.FinderGame;
import com.supinfo.sun.sungamers.gui.PlayFrame;

/**
 * @author Liu, Bing(Locke)
 * @since 2011-6-5
 * @version 0.3
 */
public class TryItActionListener implements ActionListener {

	private PlayFrame playFrame;
	
	public TryItActionListener(PlayFrame playFrame) {
		this.playFrame = playFrame;
	}
	
	public void actionPerformed(ActionEvent e) {
		FinderGame finderGame = playFrame.getFinderGame();
		String userAttempt = playFrame.getTxtField().getText();
		try {
			if(userAttempt != null && !userAttempt.trim().equals("")) {
				finderGame.addUserAttempt(userAttempt);
				if(finderGame.isWon()) {
					JOptionPane.showMessageDialog(null, "Congratulations", "Game result", 0, null);
				} else {
					JOptionPane.showMessageDialog(null, "Try again please...", "Game result", 0, null);
				}
			} else {
				JOptionPane.showMessageDialog(null, "please enter a valid value", "Game result", 0, null);
			}
		} catch (UserAttemptException e1) {
			JOptionPane.showMessageDialog(null, "Game Over", "Game result", 0, null);
		}
	}
	
}
